﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using Luminji.Core.Data;
using Luminji.Core.Data;
using Luminji.ExamWeb.Core.Domain;
using Luminji.ExamWeb.Core.Domain.Model;

namespace Luminji.ExamWeb.Infrastructure.DataAccess.Impl.SqlServer
{
    public class PaperTemplateRepository : IPaperTemplateRepository
    {
        /// <summary>
        /// 获取全部的试卷模版及结构，对结果进行Group，就可以得到全部试卷模版
        /// </summary>
        public IList<PaperTemplate> GetAllPaperTemplatesWithParts()
        {
            //--获取所有的试卷模版结构
            string sql =
                "SELECT a.PaperTemplatePartName, c.PaperTemplateName,  * FROM dbo.PaperTemplatePart a LEFT JOIN QuestionUnit b ON a.QuestionUnitCode = b.QuestionUnitCode LEFT JOIN dbo.PaperTemplate c ON a.PaperTemplateCode = c.PaperTemplateCode ORDER BY c.PaperTemplateCode, OrderValue";
            IEnumerable<PaperTemplate> pts = null;
            using (DataSet ds = SqlHelper.ExecuteDataset(SqlHelper.ConnectionString, CommandType.Text, sql))
            {
                var all = ds.Tables[0].AsEnumerable();
                //var groupSql = from p in all
                //               group p by p.Field<string>("PaperTemplateCode") into g
                //               select new { Remainder = g.Key, Numbers = g };

                pts = (from p in all
                       select new PaperTemplate()
                           {
                               PaperTemplateName = p.Field<string>("PaperTemplateName"),
                               PaperTemplateCode = p.Field<string>("PaperTemplateCode")
                           }).Distinct();

                foreach (var paperTemplate in pts)
                {
                    var paperTemplatePart = (from p in all
                                             where p.Field<string>("PaperTemplateCode") == paperTemplate.PaperTemplateCode
                                             select new PaperTemplatePart { PaperTemplateCode = p.Field<string>("PaperTemplateCode"), PaperTemplatePartCode = p.Field<string>("PaperTemplatePartCode"), PaperTemplatePartName = p.Field<string>("PaperTemplatePartName") }).ToList();
                    paperTemplate.PaperTemplateParts = paperTemplatePart as IEnumerable<PaperTemplatePart>;
                }
                
                //var temp2 = (from p in all
                //             select p.Field<string>("PaperTemplateName")).Distinct();

                //foreach (var g in groupSql)
                //{
                //    var pt = new PaperTemplate();
                //    foreach (var n in g.Numbers)
                //    {
                //        pt.PaperTemplateName = n.Field<string>("PaperTemplateName");
                //        pt.PaperTemplateCode = n.Field<string>("PaperTemplateCode");
                //        pt.SubjectCode = n.Field<string>("SubjectCode");
                //    }
                //    pts.Add(pt);
                //}

            }
            return null;
        }
    }
}
